<?php
namespace app\api\controller\v1\controller\Doctor;

use app\api\controller\v1\model\Doctor\Doctor as lm_doctor;
use think\Controller;

use app\api\controller\Api;

use think\Request;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Db;
use think\Validate;
use think\Image;
use JPush\Client as JPush;
use app\api\controller\v1\Token;
use app\api\controller\Send;

/**
 *IM
 */
class Doctor
{
    use Send;
    public function validate_doctor_name($user_name){

        $result =lm_doctor::check_doctor_exist($user_name);
        if (empty($result)) {
            return $this->returnmsg(200,"恭喜,用户名可以使用!");
        }else{
            return $this->returnmsg(401,"用户名已经被使用,请换一个昵称吧!");
        }
    }

    public function validate_doctor_phone($mobilephone){
        $result = lm_doctor::check_doctor_name_exist($mobilephone);
        if (empty($result)) {
            return $this->returnmsg(200,"恭喜,手机号可以使用!");
        }else{
            return $this->returnmsg(401,"手机号已经被使用!");
        }
    }

    public function validate_doctor_access_token($mobilephone){
        $time = date("Y-m-d H:i:s", time());
        $checkclient = Db::table('doctor_user')->field('reg_date')->where('mobilephone', $mobilephone)->find();

        $timediff = strtotime($time) - strtotime($checkclient['reg_date']);
        $time_h = $timediff / 3600;
        $result = lm_doctor::doctor_info_all($mobilephone);
        if (720 <= $time_h) {

            $new_token = Token::buildAccessToken($lenght = 32);
            lm_doctor::update_reg_time($mobilephone);
            lm_doctor::update_access_token($mobilephone, $new_token);
            $result[0]['access_token'] = $new_token;
            $result[0]['reg_date'] = now();
            return $this->returnmsg(200, '更新了ACCESS_TOKEN', $result);
        }
        return $this->returnmsg(200, 'ACCESS_TOKEN 还没有过期', $result);

    }



    //创建医生账户
    public function create_doctor_info($password,$confirm_password,$mobilephone){

        $result =lm_doctor::create_doctor_account($password,$confirm_password,$mobilephone);
        switch ($result){
            case 1:
                return $this->returnmsg(401,'用户已存在');
                break;
            case 2:
                return $this->returnmsg(402,'两次输入密码不一致');
                break;
            case 3:
                return $this->returnmsg(403,'密码应在8到16位之间');
                break;
            case 4:
                return $this->returnmsg(200,'注册成功,用户已经创建了，快去登入吧！');
                break;
            default:
                return $this->returnmsg(500,'注册失败,劳烦主子再次尝试');
        }

    }

    //查看登陆状态
    public function check_login($mobilephone){
        $status = lm_doctor::check_login_status($mobilephone);
        $login_status = $status[0]['login_status'];
        if ($login_status == 0) {
            return false;
        }else{
            return true;
        }
    }

    public function login($mobilephone,$password)
    {

        $exist = lm_doctor::check_doctor_name_exist($mobilephone);
        if (!empty($exist)) {
            if (!empty($mobilephone) && !empty($password)) {
                $status = $this->check_login($mobilephone);

                if ($status == false) {
                    $login_status = lm_doctor::update_login_status($mobilephone);

                    $result = lm_doctor::doctor_info_all($mobilephone);

                    if ($mobilephone == $result[0]['mobilephone'] && md5($password) == $result[0]['password']) {
                        if (is_null($result[0]['access_token'])) {
                            $access_token = Token::buildAccessToken($lenght = 32);
                            //插入access_token
                           if(lm_doctor::update_access_token($mobilephone, $access_token)) {
                               $result[0]['access_token'] = $access_token;
                               return $this->returnmsg(200, '登陆成功！', $result);
                           }

                            //return $this->returnmsg(200, '登陆成功！', $result);

                        } else {
                            $time = date("Y-m-d H:i:s", time());
                            $checkclient = Db::table('doctor_user')->field('last_login_time')->where('mobilephone', $mobilephone)->find();

                            $timediff = strtotime($time) - strtotime($checkclient['last_login_time']);
                            $time_h = $timediff / 3600;
                            if (720 <= $time_h) {

                                $new_token = Token::buildAccessToken($lenght = 32);
                               if( lm_doctor::update_login_time($mobilephone) && lm_doctor::update_access_token($mobilephone, $new_token) ) {
                                   $result[0]['access_token'] = $new_token;
                                   $result[0]['last_login_time'] = now();
                                   return $this->returnmsg(200, '登陆成功,且更新了ACCESS_TOKEN', $result);
                               }
                            }

                            $result = lm_doctor::doctor_info_all($mobilephone);
                            return $this->returnmsg(200, '登陆成功！', $result);

                        }
                    } else {
                        return $this->returnmsg(401, '手机号或者密码错误');
                    }

                } else { //status = ture;
                    //$login_status = ture;
                    $time = date("Y-m-d H:i:s", time());
                    $checkclient = Db::table('doctor_user')->field('last_login_time')->where('mobilephone', $mobilephone)->find();
                    $timediff = strtotime($time) - strtotime($checkclient['last_login_time']);
                    $time_h = $timediff / 3600;
                    if (720 <= $time_h) {
                        // token 过期了;
                        $new_token = Token::buildAccessToken($lenght = 32);
                        lm_doctor::update_reg_time($mobilephone);
                        lm_doctor::update_access_token($mobilephone, $new_token);
                        $result[0]['access_token'] = $new_token;
                        $result[0]['last_login_time'] = now();
                        return $this->returnmsg(200, '登陆成功,且更新了ACCESS_TOKEN', $result);

                    } else {
                        $result = lm_doctor::doctor_info_all($mobilephone);
                        return $this->returnmsg(200, '登陆成功', $result);
                    }

                }
            }
            else {
                    return $this->returnmsg(401, '手机号或者密码错误');
                }
            }
        else{
            return $this->returnmsg(400,'还没有注册哟！请先去注册吧');
        }

    }

    public function doctor_info_upload($doctor_id,$doctor_name,$doctor_gender,$doctor_age,$licence,$age_limit,$idcard)
    {

        switch ($doctor_gender) {
            case '男':
                $doctor_gender = 0;
                break;
            case '女':
                $doctor_gender = 1;
                break;
            case '保密':
                $doctor_gender = 2;
                break;
        }

        $result = lm_doctor::check_doctor_exist($doctor_name);

             if (!empty($result)) {
          return $this->returnmsg(401,'不好意思，用户名已存在！');
        }else{
             //获取上传文件
            $files = Request::instance()->file('doctor_icon');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'doctor'.DS.'doctor_icon');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $path = '/uploads/doctor_icon/' . $info->getSaveName();
                $photo = $info->getFilename();
                $small_path = ROOT_PATH . 'public' . DS. 'uploads'.DS.'doctor'.DS.'small_doctor_icon'.DS.$photo;
                //路径切割
                $path = str_replace('\\', '/', $path);
                $small_path = str_replace('\\', '/', $small_path);
                // $image
                $image = Image::open(request()->file('doctor_icon'));
                // 按照原图的比例生成一个最大为72*72的缩略图并保存为thumb.png
                $image->thumb(72, 72, Image::THUMB_CENTER)->save($small_path);

                $len = strlen(ROOT_PATH. 'public');
                $small_path = substr_replace($small_path,'',0,$len);

                if(lm_doctor::update_doctor_info($doctor_id,$doctor_name,$doctor_gender,$doctor_age,$path,$small_path,$licence,$age_limit,$idcard)){
                    //执行成功进入
                    return $this->returnmsg(200,"用户资料添加成功");
                }else {
                    return $this->returnmsg(500,"用户资料添加失败");
                }

            }
        }
    }

    public function upload_certification_postive_img ($doctor_id)
    {
        $file = Request::instance()->file('doctor_certification_img');

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'doctor' . DS . 'doctor_certification');
            if ($info) {
                $path = '/uploads/doctor/doctor_certification/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);

                Db::query("update doctor_user set certification_postive = '$$path' where doctor_id = '$doctor_id'");

        }
        return $this->returnmsg(200,"医生证明上传成功");
    }

    public function upload_certification_negative_img ($doctor_id)
    {
        $file = Request::instance()->file('doctor_certification_img');

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'doctor' . DS . 'doctor_certification');
            if ($info) {
                $path = '/uploads/doctor/doctor_certification/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);

                Db::query("update doctor_user set certification_negative = '$$path' where doctor_id = '$doctor_id'");
            }
        return $this->returnmsg(200,"医生证明上传成功");
    }

    public function upload_identification_postive_img ($doctor_id){
        $file = Request::instance()->file('doctor_identification_img');

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'doctor' . DS . 'doctor_identification');
            if ($info) {
                $path = '/uploads/doctor/doctor_identification/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);

                Db::query("update doctor_user set identification_postive = '$$path' where doctor_id = '$doctor_id'");
            }
        return $this->returnmsg(200,"医生身份证正面上传成功");
    }

    public function upload_identification_negative_img ($doctor_id){
        $file = Request::instance()->file('doctor_identification_img');

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'doctor' . DS . 'doctor_identification');
            if ($info) {
                $path = '/uploads/doctor/doctor_identification/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);

                Db::query("update doctor_user set identification_negative = '$$path' where doctor_id = '$doctor_id'");
            }
        return $this->returnmsg(200,"医生身份证面反上传成功");
    }

    public function Approve_doctor ($doctor_id){
        if ( lm_doctor::check_doctor_approvement($doctor_id)){
            return $this->returnmsg(200, '审核通过！');
        }

    }



    /**
     * 医生端修改状态
     * @param $mobilephone 手机号
     * @return mixed
     */
    public function logchange($doctor_id, $status){
        $result = lm_doctor::change_logstas($doctor_id,$status);
        //判断返回值状态
        if($result['status'])
        {
            return $this->returnmsg(200,'修改成功',['status'=>$status]);

        }
        return $this->returnmsg(400,'操作失败');
    }










}